package com.huike.clues.service.impl;

import java.util.List;

import com.alibaba.excel.util.CollectionUtils;
import com.huike.clues.domain.Activity;
import com.huike.clues.domain.FalseClues;
import com.huike.clues.domain.HkBusiness;
import com.huike.clues.mapper.FalseCluesMapper;
import com.huike.clues.mapper.HkBusinessMapper;
import com.huike.clues.service.IActivityService;
import com.huike.clues.service.IFalseCluesService;
import com.huike.common.PrimaryKeyUtil;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.uuid.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huike.clues.mapper.CluesMapper;
import com.huike.clues.domain.Clues ;
import com.huike.clues.service.ICluesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 线索Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CluesServiceImpl implements ICluesService
{
    @Autowired
    private CluesMapper cluesMapper;
    @Autowired
    private IFalseCluesService falseCluesService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private FalseCluesMapper falseCluesMapper;
    @Autowired
    private HkBusinessMapper hkBusinessMapper;
    @Autowired
    private PrimaryKeyUtil primaryKeyUtil;

    /**
     * 查询线索
     * 
     * @param id 线索主键
     * @return 线索
     */
    @Override
    public Clues selectCluesById(String id)
    {
        return cluesMapper.selectCluesById(id);
    }

    /**
     * 查询线索列表
     * 
     * @param clues 线索
     * @return 线索
     */
    @Override
    public List<Clues> selectCluesList(Clues clues)
    {
        return cluesMapper.selectCluesList(clues);
    }

    /**
     * 新增线索
     * 
     * @param clues 线索
     * @return 结果
     */
    @Override
    public int insertClues(Clues clues)
    {
        String prefix = primaryKeyUtil.getOrderIdPrefix(DateUtils.getNowDate());
        Long id = primaryKeyUtil.orderId(prefix);
        clues.setId(id);
        Activity activity = activityService.selectActivityById(clues.getActivityId());
        clues.setActivityName(activity.getName());
        clues.setCreateBy(SecurityUtils.getUsername());
        Long ownerId = SecurityUtils.getUserId();
        int count = cluesMapper.selectCountByOwnerId(ownerId);
        if (count==3){
            clues.setClueStatus(1);
        }else {
            clues.setOwnerId(ownerId);
        }
        clues.setCreateTime(DateUtils.getNowDate());
        return cluesMapper.insertClues(clues);
    }

    /**
     * 修改线索
     * 
     * @param clues 线索
     * @return 结果
     */
    @Override
    public int updateClues(Clues clues)
    {
        clues.setUpdateTime(DateUtils.getNowDate());
        return cluesMapper.updateClues(clues);
    }

    /**
     * 批量删除线索
     * 
     * @param ids 需要删除的线索主键
     * @return 结果
     */
    @Override
    public int deleteCluesByIds(String[] ids)
    {
        return cluesMapper.deleteCluesByIds(ids);
    }

    /**
     * 删除线索信息
     * 
     * @param id 线索主键
     * @return 结果
     */
    @Override
    public int deleteCluesById(String id)
    {
        return cluesMapper.deleteCluesById(id);
    }

    @Override
    public List<Clues> selectCluesPoolList(Clues clues) {
        return cluesMapper.selectCluesPoolList(clues);
    }
    /**
     * 伪线索上报
     */
    @Override
    public AjaxResult updateClueFalse(Long id, FalseClues falseClues) {
        List<FalseClues> list=falseCluesService.selectClueFalseByClueId(id);
        //判断已上报次数是否为达到三次
        if(!CollectionUtils.isEmpty(list)){
            //达到三次则删除该条线索
            if (list.size()>=2){
                int i = cluesMapper.deleteCluesById(id.toString());
                falseCluesService.deleteFalseCluesByClueId(id);
                return AjaxResult.success("上报次数达到三次,已删除", i);
            }
        }
        falseClues.setClueId(id);
        falseCluesService.insertFalseClues(falseClues);
        cluesMapper.updateClueFalse(id);
        return AjaxResult.success();
    }

    /**
     * 捞取线索
     *
     * @param ids 线索主键
     * @return 结果
     */
    @Override
    public int updateOwner(List<Long> ids) {
        int size = ids.size();
        if(size>3) {
            throw new BaseException("捞取线索超过拥有最大值3条");
        }
        Long ownerId = SecurityUtils.getUserId();
        int i = cluesMapper.selectCountByOwnerId(ownerId);
        int count=i+size;
        if (count>3){
            throw new BaseException("您已拥有"+i+"条线索,拥有最大值为3条");
        }
        return cluesMapper.updateOwner(ids,ownerId);
    }

    /**
     * 转商机
     *
     * @param id 线索主键
     * @return 结果
     */
    @Override
    public int changeBusiness(String id) {
        Clues clue = cluesMapper.selectCluesById(id);
        HkBusiness business = new HkBusiness();
        BeanUtils.copyProperties(clue, business);
        business.setStatus(4L);
        Long age = Long.valueOf(clue.getAge());
        business.setAge(age);
        cluesMapper.deleteCluesById(id);
        return hkBusinessMapper.insertHkBusiness(business);
    }

}
